package com.oa.ylms.apps.dao.regulations.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.oa.ylms.apps.dao.regulations.IRegulationInfoDao;
import com.oa.ylms.apps.entity.archives.ArchiveInfoValue;
import com.oa.ylms.apps.entity.regulations.RegulationInfoValue;

public class RegulationInfoDaoImpl extends HibernateDaoSupport implements IRegulationInfoDao {

	private List<RegulationInfoValue> list;
	
	private Integer numbers;
	
	// DAO method
	@SuppressWarnings("unchecked")
	@Override
	public int countTPages(final RegulationInfoValue regulationInfo, final ArchiveInfoValue archiveInfo, int rows)
			throws Exception {
		int pageNumbers = 0;
		String hqlStr = "SELECT riv FROM RegulationInfoValue riv ,StaffInfoValue siv, ArchiveInfoValue aiv WHERE 1=1 AND siv.staffId=riv.staffInfo.staffId AND siv.archiveInfo.archiveId = aiv.archiveId";
		if (regulationInfo.getRegulationCategory() != null) {
			hqlStr += " AND riv.regulationCategory = ?";
		}
		if (regulationInfo.getPublishTime() != null){
			hqlStr += " AND riv.publishTime <= ?";
		}
		if (regulationInfo.getTitle() != null){
			hqlStr += " AND riv.title like ?";
		}
		if(archiveInfo.getStaffName() != null){
			hqlStr += " AND aiv.staffName like ?";
		}
		hqlStr += " ORDER BY riv.publishTime DESC";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						int index = -1;
						if (regulationInfo.getRegulationCategory() != null) {
							query.setEntity(++index, regulationInfo.getRegulationCategory());
						}
						if (regulationInfo.getPublishTime() != null){
							query.setDate(++index, regulationInfo.getPublishTime());
						}
						if (regulationInfo.getTitle() != null){
							query.setString(++index, regulationInfo.getTitle());
						}
						if (archiveInfo.getStaffName() != null){
							query.setString(++index, archiveInfo.getStaffName());
						}
						list = query.list();
						return list;
					}
				});
		numbers = list.size();
		if (numbers <= rows) {
			pageNumbers = 1;
		} else {
			if (numbers % rows == 0) {
				pageNumbers = numbers / rows;
			} else {
				pageNumbers = numbers / rows + 1;
			}
		}
		return pageNumbers;
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<RegulationInfoValue> qureyRegulationsWithPage(final RegulationInfoValue regulationInfo, final ArchiveInfoValue archiveInfo, final int rows,final int pageNo) throws Exception {
		String hqlStr = "SELECT riv FROM RegulationInfoValue riv ,StaffInfoValue siv, ArchiveInfoValue aiv WHERE 1=1 AND siv.staffId=riv.staffInfo.staffId AND siv.archiveInfo.archiveId = aiv.archiveId";
		if (regulationInfo.getRegulationCategory()!= null) {
			hqlStr += " AND riv.regulationCategory = ?";
		}
		if (regulationInfo.getPublishTime() != null){
			hqlStr += " AND riv.publishTime <= ?";
		}
		if (regulationInfo.getTitle() != null){
			hqlStr += " AND riv.title like ?";
		}
		if(archiveInfo.getStaffName() != null){
			hqlStr += " AND aiv.staffName like ?";
		}
		hqlStr += " ORDER BY riv.publishTime DESC";
		final String hql = hqlStr;
		list = this.getHibernateTemplate().executeFind(
				new HibernateCallback() {
					public Object doInHibernate(Session session)
							throws HibernateException, SQLException {
						Query query = session.createQuery(hql);
						int index = -1;
						if (regulationInfo.getRegulationCategory() != null) {
							query.setEntity(++index, regulationInfo.getRegulationCategory());
						}
						if (regulationInfo.getPublishTime() != null){
							query.setDate(++index, regulationInfo.getPublishTime());
						}
						if (regulationInfo.getTitle() != null){
							query.setString(++index, regulationInfo.getTitle());
						}
						if (archiveInfo.getStaffName() != null){
							query.setString(++index, archiveInfo.getStaffName());
						}
						query.setFirstResult(rows * (pageNo - 1));
						query.setMaxResults(rows);
						list = query.list();
						return list;
					}
				});
		return list;
	}


	@Override
	public RegulationInfoValue qureyRegulationById(RegulationInfoValue regulationInfo)throws Exception {

		return (RegulationInfoValue) this.getHibernateTemplate().get(RegulationInfoValue.class, regulationInfo.getRegulationId());
	}


	@SuppressWarnings("unchecked")
	@Override
	public List<RegulationInfoValue> qureyTopRegulation(int topNum)
			throws Exception {
		String hql = "SELECT TOP ? riv FROM NewsInfoValue riv ORDER BY riv.publishTime DESC";
		return this.getHibernateTemplate().find(hql,topNum);
	}
	
	

	@Override
	public int addRegulationInfo(RegulationInfoValue regulationInfo)throws Exception {
		if (regulationInfo != null) {
			this.getHibernateTemplate().save(regulationInfo);
			return regulationInfo.getRegulationId();
		}
		return -1;
	}

	
	@Override
	public boolean modifyRegulationInfo(RegulationInfoValue regulationInfo)throws Exception {
		if (regulationInfo != null) {
			try {
				this.getHibernateTemplate().update(regulationInfo);
			} catch (Exception e) {
			e.printStackTrace();
			}
			return true;
		}
		return false;
	}
	
	@Override
	public boolean deleteRegulationInfo(RegulationInfoValue regulationInfo)throws Exception {
		if (regulationInfo != null) {
			try {
				this.getHibernateTemplate().delete(regulationInfo);
			} catch (Exception e) {
			e.printStackTrace();
			}
			return true;
		}
		return false;
	}
	
	@Override
	public int getTotalNumbers() throws Exception{
		return getNumbers();
	}

	// DAO property accessors
	
	public List<RegulationInfoValue> getList() {
		return list;
	}

	public void setList(List<RegulationInfoValue> list) {
		this.list = list;
	}


	public Integer getNumbers() {
		return numbers;
	}


	public void setNumbers(Integer numbers) {
		this.numbers = numbers;
	}

	

	
	
}
